package com.anzia.study_2.slice;

import com.anzia.study_2.ResourceTable;
import com.anzia.study_2.utils.DeviceUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;

public class MigrationAbilitySlice extends AbilitySlice implements IAbilityContinuation {

	private TextField textField;
	private Button mgBtn;
	private String tfStr = "";

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_migration);

		// 编辑的文字
		textField = (TextField) findComponentById(ResourceTable.Id_textfield_migration);
		// 默认为空
		textField.setText(tfStr);

		mgBtn = (Button) findComponentById(ResourceTable.Id_migration_button);
		mgBtn.setClickedListener(component -> {
			// 核心代码，点击按钮，实现FA的迁移
			// 第一步获取设备id
			String deviceId = DeviceUtils.getDeviceId();
			if (deviceId != null) {
				// 实现设备迁移
				continueAbility(deviceId);
			}
		});

	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}

	@Override
	public boolean onStartContinuation() {
		return true;
	}

	@Override
	// 保持数据
	public boolean onSaveData(IntentParams intentParams) {
		// 获得文本框中的文字，并保存到data中，键值对形式
		intentParams.setParam("data", textField.getText());
		return true;
	}

	@Override
	// 恢复数据
	public boolean onRestoreData(IntentParams intentParams) {
		// 在其他设备恢复数据时，从data读取内容
		tfStr = intentParams.getParam("data").toString();
		return true;
	}

	@Override
	public void onCompleteContinuation(int i) {

	}
}
